package com.sumainfor.pc.market.controller.tenant;

import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.market.service.tenant.SignedProviderService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author Eddy·Chen
 * @Email 835033913@qq.com
 * @Create 2019-02-25 17:57
 * @Document 合同签约提供者
 */
@RestController
@RequestMapping("/tenant/key/signed")
public class SignedProviderController {

    Logger log= LoggerFactory.getLogger(SignedProviderController.class);

    @Autowired
    private SignedProviderService sps;

    /**
     * 通知消息回调接口 异步
     *
     * @param params
     * @return
     */
    @PostMapping("/noticeStateResult")
    public JsonResult noticeStateResult(@RequestBody Map<String, Object> params) {
        return sps.noticeStateResult(params);
    }

    /**
     * 查询等待签约的数据列表
     * @return
     */
    @ResponseBody
    @PostMapping("/getWaitSignList")
    public JsonResult getWaitSignList(@RequestBody Map<String,Object>params) {
        JsonResult result=new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"TenId"})){
            result.putFailed("TenId不能为空！");
            return  result;
        }
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"uopid"})){
            result.putFailed("uopid不能为空！");
            return  result;
        }
        params.put("FlowStatus",1);
        result.put(sps.getWaitSignList(params));
        return result;
    }

    /**
     * 功能描述:
     * 根据合同编号获取合同状态
     * FlowStatus 电子合同签署状态  \n1.签署中\n2:签署完成\n3:失败\n4:拒签
     * IfFlowPay  电子合同是否支付     0无状态   1已完成未支付    2已完成已支付（该状态只有电子合同状态为2才有用）
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/20 10:32
     */
    @GetMapping("/getPactStatus")
    public JsonResult getPactStatus(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();

        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"PaId"})){
            result.putFailed("PaId不能为空！");
            return  result;
        }
        result.put(sps.getPactStatus(params));
        return result;
    }

    /**
     * 查询签约流程第一个流程的数据
     * @return
     */
    @PostMapping("/getPactSignFlowOne")
    public JsonResult getPactSignFlowOne(@RequestBody Map<String,Object>params) {
        JsonResult result=new JsonResult();
        params.put("delfg",0);
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"PaId"})){
            result.putFailed("PaId不能为空！");
            return  result;
        }
        result.put(sps.getPactSignFlowOne(params));
        return result;
    }

    /**
     * 查询签约流程第二步数据;
     *
     * @param pactId
     * @return
     */
    @PostMapping("/getPactSignFlowTwo")
    public JsonResult getPactSignFlowTwo(String pactId) {
        if (StringUtils.isBlank(pactId)) {
            return JsonResult.errorForEmpty();
        }
        return sps.getPactSignFlowTwo(pactId);
    }

    /**
     * 查询签约流程第三步和第四步数据;
     *
     * @param pactId
     * @return
     */
    @PostMapping("/getPactSignThreeAndFour")
    public JsonResult getPactSignThreeAndFour(String pactId) {
        if (StringUtils.isBlank(pactId)) {
            return JsonResult.errorForEmpty();
        }
        return sps.getPactSignThreeAndFour(pactId);
    }


    /**
     * 查询签约流程第五步数据;
     *
     * @param pactId
     * @return
     */
    @PostMapping("/getPactSignFive")
    public JsonResult getPactSignFive(String pactId) {
        if (StringUtils.isBlank(pactId)) {
            return JsonResult.errorForEmpty();
        }
        return sps.getPactSignFive(pactId);
    }

    /**
     * 功能描述: 
     * 根据合同编号获取合同第八步资料
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/20 14:26
     */
    @GetMapping("/getPactSignEight")
    public JsonResult getPactSignEight(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        params.put("delfg",0);
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"PaId"})){
            result.putFailed("PaId不能为空！");
            return  result;
        }
        result.put(sps.getPactSignEight(params));
        return result;
    }





    /**
     * 功能描述: 
     * 根据租客编号获取租客合同列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/23 12:02
     */
    @GetMapping("/getPactList")
    public JsonResult getPactList(@RequestParam Map<String,Object>params,Pager pager){
        JsonResult result=new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"TenId"})){
            result.putFailed("TenId不能为空！");
            return  result;
        }
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        Integer cout=sps.getPactListCount(params);
        if(cout==0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,cout);
        }
        List<Map<String,Object>> getActivityList=sps.getPactList(params);
        result=pageUtils.getJsonResult(getActivityList, params, cout);

        return result;
    }
}
